import axios from 'axios'
// import store from '@/store'
// import { getToken } from '@/utils/auth'
import { Toast } from 'vant'
// import { host } from '@/api/config'

const baseURL = process.env.NODE_ENV === 'development' ? '/api' : process.env.VUE_APP_BASE_API

const service = axios.create({
  baseURL,//
  timeout: 120000
})


// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    // console.log('-->', store.state.user.TOKEN);
    let token = undefined
    if (JSON.parse(localStorage.getItem("userInfo"))) {
      const info = JSON.parse(localStorage.getItem("userInfo"))
      token = info.token
      // console.log('token', token);
    }

    // console.log(info.token);
    // console.log('userInfo', userInfo);
    // console.log('userInfo', userInfo.token);
    if (token) {
      config.headers['mmc-identity'] = token
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    let { status, message } = response.data
    if (status === 1) {
      return Promise.resolve(response.data)
    } else {
      Toast.fail(message)
      return Promise.resolve(response.data)
    }
  },
  error => {
    console.log('err' + error)
    Toast.fail(error.message)
  }
)

export default service
